package com.ochnas.volunteersignup.ui;

import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Pattern;

import javax.persistence.NonUniqueResultException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.UserService;

public class NewAccountFormHandler {
	public static ApplicationContext spring = 
		new ClassPathXmlApplicationContext("applicationContext.xml");
	private UserService userService = (UserService) spring.getBean("userService");
	private Map<String, String> errors = new Hashtable<String, String>();
	private static Pattern emailAddressPattern = Pattern.compile("[^\\s@]+[@{1}+][^\\s@]+");	
	private static Pattern threeDigitsPattern = Pattern.compile("[\\d]{3}+");
	private static Pattern fourDigitsPattern = Pattern.compile("[\\d]{4}+");
	private static String emailAddressProperty = "emailAddress";
	private static String password1Property = "password1";
	private static String password2Property = "password2";
	private static String firstNameProperty = "firstName";
	private static String lastNameProperty = "lastName";
	private static String phoneNumberProperty = "phoneNumber";
	private String emailAddress = "";
	private String password1 = "";
	private String password2 = "";
	private String firstName = "";
	private String lastName = "";
	private String areaCode = "";
	private String digits1 = "";
	private String digits2 = "";

	public NewAccountFormHandler() {
		super();
	}

	
	public boolean validate() {
		errors.clear();
		
		boolean isValid = true;
		if (emailAddress == null || (emailAddress != null && emailAddress.equals(""))) {
			errors.put(emailAddressProperty, "Please enter email address.");
			isValid = false;
		} else if (!emailAddressPattern.matcher(emailAddress).matches()) {
			errors.put(emailAddressProperty, "Please enter a valid email address.");
			isValid = false;
		} else {
			if (errors.containsKey(emailAddressProperty)) errors.remove(emailAddressProperty);
		}
		
		if (password1 == null || (password1 != null && password1.equals(""))) {
			password1 = "";
			errors.put(password1Property, "Please enter password.");
			isValid = false;
		} else {
			if (errors.containsKey(password1Property)) errors.remove(password1Property);
		}
		
		if (password2 == null || (password2 != null && password2.equals(""))) {
			password2 = "";
			errors.put(password2Property, "Please enter password.");
			isValid = false;
		} else {
			if (errors.containsKey(password2Property)) errors.remove(password2Property);
		}
		
		if (!(errors.containsKey(password1Property) || errors.containsKey(password2Property))) {
			if (!password1.equals(password2)) {
				password1 = "";
				password2 = "";
				errors.put(password1Property, "Passwords do not match.");
			} else {
				if (errors.containsKey(password1Property)) errors.remove(password1Property);
			}
		}
		

		if (firstName == null || (firstName != null && firstName.equals(""))) {
			errors.put(firstNameProperty, "Please enter your first name.");
			isValid = false;
		} else {
			if (errors.containsKey(firstNameProperty)) errors.remove(firstNameProperty);
		}
		
		if (lastName == null || (lastName != null && lastName.equals(""))) {
			errors.put(lastNameProperty, "Please enter your last name.");
			isValid = false;
		} else {
			if (errors.containsKey(lastNameProperty)) errors.remove(lastNameProperty);
		}
		
		if ((areaCode == null || (areaCode != null && areaCode.equals(""))) ||
				(digits1 == null || (digits1 != null && digits1.equals(""))) ||
				(digits2 == null || (digits2 != null && digits2.equals(""))) ||
				(areaCode != null && !threeDigitsPattern.matcher(areaCode).matches()) ||
				(digits1 != null && !threeDigitsPattern.matcher(digits1).matches()) ||
				(digits2 != null && !fourDigitsPattern.matcher(digits2).matches())) {
			errors.put(phoneNumberProperty, "Please enter your phone number.");
			isValid = false;
		} else {
			if (errors.containsKey(phoneNumberProperty)) errors.remove(phoneNumberProperty);
		}
		
		return isValid;
	}
	
	
	public User createUserAccount() {
		User u = null;
		try {
			u = userService.createUserAccount(emailAddress, password1, firstName, lastName, areaCode+digits1+digits2);
		} catch (NonUniqueResultException e) {
			errors.put("emailAddress", e.getMessage());
		}
		return u;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getDigits1() {
		return digits1;
	}

	public void setDigits1(String digits1) {
		this.digits1 = digits1;
	}

	public String getDigits2() {
		return digits2;
	}

	public void setDigits2(String digits2) {
		this.digits2 = digits2;
	}
	
	public String getErrorMsg(String key) {
		String value = errors.get(key.trim());
		return (value==null?"":value);
	}
	
	public void setErrorMsg(String key, String value) {
		errors.put(key, value);
	}
	

}
